﻿package com.work.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.work.domain.CompanyIndexInfo;
import com.work.domain.CompanyInfo;
import com.work.domain.CompanySearchtype;
import com.work.domain.CompanyTopInfo;
import com.work.domain.UserAdmin;
import com.work.service.CompanyInfoService;
import com.work.util.GetUserSessionTool;
import com.work.util.PageUtil;
import com.work.util.Constant;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @author zhubingyi
 * @version 1.0
 * @description pc服务端接口
 *
 */
@Controller
@RequestMapping("/companyWeb")
public class CompanyInfoController{
    //本地地址
    static private String remoteAddr=Constant.LOCAL_ADDRESS;
    
    //常量：http：//
    static private String http=Constant.HTTP;
	
    
	@Resource
	private CompanyInfoService companyInfoService;
	
	//跳转 后台公司信息添加页面
	@RequestMapping(value="goCompanyListAddWeb",produces="text/html;charset=UTF-8")
	public String goCompanyListAddWeb(Model model,HttpServletRequest request){
		UserAdmin useradmin = GetUserSessionTool.getUserSession(request);
		if(useradmin==null||"".equals(useradmin)){
			model.addAttribute("ngworkusererror", "用户登录已超时，请重新登录！");
			return "fail";
		}else{
			return "forward:/jsps/company/companyListAdd.jsp";
		}
	}
	
	//查询长期招聘公司全部信息
	@RequestMapping(value="companyListWeb",produces="text/html;charset=UTF-8")
	public String companyList(Model model,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
			response.setContentType("text/plain; charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			//验证session是否过期
			UserAdmin useradmin = GetUserSessionTool.getUserSession(request);
			if(useradmin==null||"".equals(useradmin)){
				model.addAttribute("ngworkusererror", "用户登录已超时，请重新登录！");
				return "fail";
			}else{
				List<CompanyInfo> list=companyInfoService.findAllCompanyInfo();
				JSONArray jsonArray = JSONArray.fromObject(list);
				model.addAttribute("companyList", jsonArray);
			    return "company/companyList";
			}
	}
	//查询长期招聘公司后台首页信息
	@RequestMapping(value="companyIndexListWeb",produces="text/html;charset=UTF-8")
	public String companyIndexList(Model model,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
			response.setContentType("text/plain; charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			String contextPath = request.getContextPath();
			String localPort = String.valueOf(request.getLocalPort());
			//验证session是否过期
			UserAdmin useradmin = GetUserSessionTool.getUserSession(request);
			if(useradmin==null||"".equals(useradmin)){
				model.addAttribute("ngworkusererror", "用户登录已超时，请重新登录！");
				return "fail";
			}else{
				List<CompanyIndexInfo> list=companyInfoService.findAllCompanyIndexInfo();
				for (CompanyIndexInfo cii : list) {
					String cphoto = cii.getCphoto();
					String concat = remoteAddr.concat(request.getContextPath());
					String cphotoPath =concat.concat(cphoto);
					cii.setCphoto(http.concat(cphotoPath));
				}
				JSONArray jsonArray = JSONArray.fromObject(list);
				model.addAttribute("companyIndexList", jsonArray);
			    return "company/companyIndexList";
			}
	}
	//查询长期招聘公司首页推荐位信息
	@RequestMapping(value="companyTopListWeb",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String companyTopList(Model model,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
			response.setContentType("text/plain; charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			String localPort = String.valueOf(request.getLocalPort());
			String contextPath = request.getContextPath();
			//验证session是否过期
			UserAdmin useradmin = GetUserSessionTool.getUserSession(request);
			if(useradmin==null||"".equals(useradmin)){
				model.addAttribute("ngworkusererror", "用户登录已超时，请重新登录！");
				return "fail";
			}else{
				List<CompanyTopInfo> list=companyInfoService.findAllCompanyTopIndexInfo();
				JSONArray jsonArray = null;
				for (CompanyTopInfo cii : list) {
					String cphoto = cii.getCphoto();
					String concat = remoteAddr.concat(request.getContextPath());
					String cphotoPath =concat.concat(cphoto);
					cii.setCphoto(http.concat(cphotoPath));
				}
				jsonArray = JSONArray.fromObject(list);
				//System.out.println(jsonArray.toString());
				JSONObject data = new JSONObject();
				JSONObject ob=new JSONObject();
				    data.put("workList", jsonArray);
				    ob.put("code", 0);
				    ob.put("data",data );
				    ob.put("msg", "查询成功");
				//model.addAttribute("companyList", list);
			    return ob.toString();
			}
	}
	
	//查询长期招聘企业详细信息
	@RequestMapping(value="companyInfoByCidWeb",produces="text/html;charset=UTF-8")
	public String companyInfoByCid(Model model,CompanyInfo ci,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
			response.setContentType("text/plain; charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			Integer cid = ci.getCid();
			//System.out.println(cid+"    id");
			//验证session是否过期
			UserAdmin useradmin = GetUserSessionTool.getUserSession(request);
			if(useradmin==null||"".equals(useradmin)){
				model.addAttribute("ngworkusererror", "用户登录已超时，请重新登录！");
				return "fail";
			}else{
				CompanyInfo companyInfo=companyInfoService.findCompanyInfoByCid(cid);
				model.addAttribute("cdWeb", companyInfo);
				return "company/companyDetail";
			}
	}
	//根据条件查询企业列表信息
	@RequestMapping(value="companyListByConditionsWeb",produces="text/html;charset=UTF-8")
	public String companyListByConditions(Model model,CompanyInfo companyInfo,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
			response.setContentType("text/plain; charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			String percent="%";
			String cname = companyInfo.getCname();
			String caddress = companyInfo.getCaddress();
			String ccount = companyInfo.getCcount();
			String clabourcost = companyInfo.getClabourcost();
			//公司名称
			cname =percent.concat(cname).concat(percent);
			//公司地址
			caddress =percent.concat(caddress).concat(percent);
			//公司规模
			ccount =percent.concat(ccount).concat(percent);
			//优惠政策
			clabourcost =percent.concat(clabourcost).concat(percent);
			//验证session是否过期
			UserAdmin useradmin = GetUserSessionTool.getUserSession(request);
			if(useradmin==null||"".equals(useradmin)){
				model.addAttribute("ngworkusererror", "用户登录已超时，请重新登录！");
				return "fail";
			}else{
				List<CompanyInfo> list=companyInfoService.findCompanyListByConditionsWeb(cname,caddress,ccount,clabourcost);
				JSONArray jsonArray = JSONArray.fromObject(list);
				model.addAttribute("companyList", jsonArray);
			    return "company/companyList";
			}
	}
	//长期招聘公司信息删除
	@RequestMapping(value="deleteCompanyWeb",produces="text/html;charset=UTF-8")
	public void deleteCompanyWeb(Model model,HttpServletRequest request,HttpServletResponse response){
		String cids = request.getParameter("ids");
		JSONArray fo = JSONArray.fromObject(cids);
		int flag=0;
		PrintWriter writer = null;
		Integer deleteInfo=null;
		//验证session是否过期
		UserAdmin useradmin = GetUserSessionTool.getUserSession(request);
		if(useradmin==null||"".equals(useradmin)){
			writer.print("用户登录已超时，请重新登录！");
			writer.flush();
			writer.close();
		}else{
			try {
			    for (Object object : fo) {
					writer = response.getWriter();
					flag++;
					int cid = Integer.parseInt(object.toString());
					deleteInfo= companyInfoService.deleteCompanyById(cid);
				   }
				if(flag==fo.size()){
						if(deleteInfo==1){
							writer.print("1");
							writer.flush();
						}
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					writer.close();
				}
		}
	}
	//修改长期招聘公司信息
	@RequestMapping(value="updateCompanyInfoWeb",produces="text/html;charset=UTF-8")
    public String updateCompanyInfo(Model model,CompanyInfo companyInfo,HttpServletRequest request){
		//验证session是否过期
		UserAdmin useradmin = GetUserSessionTool.getUserSession(request);
		if(useradmin==null||"".equals(useradmin)){
			model.addAttribute("ngworkusererror", "用户登录已超时，请重新登录！");
			return "fail";
		}else{
	        companyInfoService.updateCompanyInfo(companyInfo);
	        //更新完成跳转查询 公司列表
	        List<CompanyInfo> companyList = companyInfoService.findAllCompanyInfo();
	        JSONArray jsonArray = JSONArray.fromObject(companyList);
	    	model.addAttribute("companyList",jsonArray);
	    	return "company/companyList";
		}
    }
	//添加长期招聘公司信息
	@RequestMapping(value="addCompanyInfoWeb",produces="text/html;charset=UTF-8")
	@ResponseBody
	public void addCompanyInfoWeb(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		response.setContentType("text/plain; charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		JSONArray json = JSONArray.fromObject(request.getParameter("companys"));
		int flag=0;
		PrintWriter writer = null;
		Integer saveInfo=null;
		//验证session是否过期
		UserAdmin useradmin = GetUserSessionTool.getUserSession(request);
		if(useradmin==null||"".equals(useradmin)){
			writer.print("用户登录已超时，请重新登录！");
			writer.flush();
			writer.close();
		}else{
			try {
	          for (Object object : json) {
	        	writer = response.getWriter();
				flag++;
				JSONObject job = JSONObject.fromObject(object);
				String cname = (String) job.get("cname");
				String ccount = (String) job.get("ccount");
				String csalaryl = (String) job.get("csalaryl");
				String csalaryh = (String) job.get("csalaryh");
				String clabourcost = (String) job.get("clabourcost");
				String workaddress = (String) job.get("workaddress");
				String cpaydate = (String) job.get("cpaydate");
				String cpaydetail = (String) job.get("cpaydetail");
				String cmeals = (String) job.get("cmeals");
				String chousing = (String) job.get("chousing");
				String ctraffic = (String) job.get("ctraffic");
				String cworktime = (String) job.get("cworktime");
				String cworkenvironment = (String) job.get("cworkenvironment");
				String ctrain = (String) job.get("ctrain");
				String cinformation = (String) job.get("cinformation");
				String caddress = (String) job.get("caddress");
				String cno = (String) job.get("cno");
				String ccontract = (String) job.get("ccontract");
				String cpayexplain = (String) job.get("cpayexplain");
				String cinsurance = (String) job.get("cinsurance");
				String cphoto = (String) job.get("cphoto");
				String remark = (String) job.get("remark");
				
				CompanyInfo ci=new CompanyInfo();
				ci.setCname(cname);
				ci.setCaddress(caddress);
				ci.setCcontract(ccontract);
				ci.setCcount(ccount);
				ci.setChousing(chousing);
				ci.setCinformation(cinformation);
				ci.setCinsurance(cinsurance);
				ci.setClabourcost(clabourcost);
				ci.setCmeals(cmeals);
	            ci.setCno(cno);
	            ci.setCpaydate(cpaydate);
	            ci.setCpaydetail(cpaydetail);
	            ci.setCpayexplain(cpayexplain);
	            ci.setCphoto(cphoto);
	            ci.setCsalaryh(csalaryh);
	            ci.setCsalaryl(csalaryl);
	            ci.setCtraffic(ctraffic);
	            ci.setCtrain(ctrain);
	            ci.setCworkenvironment(cworkenvironment);
	            ci.setCworktime(cworktime);
	            ci.setRemark(remark);
	            
				//执行添加 公司信息
				saveInfo=companyInfoService.saveCompanyInfo(ci);
	             }
				if(flag==json.size()){
						if(saveInfo==1){
							writer.print("1");
							writer.flush();
						}else{
							writer.print("添加失败！");
						}
				    }
	            } catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					writer.close();
				}
		     }
	    }
	//查询长期招聘公司模糊搜索类型
	@RequestMapping(value="companySearchTypeListWeb",produces="text/html;charset=UTF-8")
	public String companySearchTypeList(Model model,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		response.setContentType("text/plain; charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		//验证session是否过期
		UserAdmin useradmin = GetUserSessionTool.getUserSession(request);
		if(useradmin==null||"".equals(useradmin)){
			model.addAttribute("ngworkusererror", "用户登录已超时，请重新登录！");
			return "fail";
		}else{
			List<CompanyIndexInfo> list=companyInfoService.findCompanySearchType();
			JSONArray jsonArray = JSONArray.fromObject(list);
			model.addAttribute("companySearchType", jsonArray);
			return "company/companySearchType";
		}
	}
	//添加长期招聘公司模糊搜索类型
	@RequestMapping(value="addCompanySearchTypeWeb",produces="text/html;charset=UTF-8")
	public String addCompanySearchTypeWeb(Model model,CompanySearchtype companySearchtype,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		response.setContentType("text/plain; charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		//验证session是否过期
		UserAdmin useradmin = GetUserSessionTool.getUserSession(request);
		if(useradmin==null||"".equals(useradmin)){
			model.addAttribute("ngworkusererror", "用户登录已超时，请重新登录！");
			return "fail";
		}else{
			Integer flag= companyInfoService.addCompanySearchTypeWeb(companySearchtype);
			if(flag==1){
				List<CompanyIndexInfo> list=companyInfoService.findCompanySearchType();
				JSONArray jsonArray = JSONArray.fromObject(list);
				model.addAttribute("companySearchType", jsonArray);
			}
			return "company/companySearchType";
		}
	}
	//删除长期招聘公司模糊搜索类型
	@RequestMapping(value="deleteCompanySearchWeb",produces="text/html;charset=UTF-8")
	public void deleteCompanySearchWeb(Model model,HttpServletRequest request,HttpServletResponse response){
		String cids = request.getParameter("ids");
		JSONArray fo = JSONArray.fromObject(cids);
		int flag=0;
		PrintWriter writer = null;
		Integer deleteInfo=null;
		//验证session是否过期
		UserAdmin useradmin = GetUserSessionTool.getUserSession(request);
		if(useradmin==null||"".equals(useradmin)){
			writer.print("用户登录已超时，请重新登录！");
			writer.flush();
			writer.close();
		}else{
			try {
			    for (Object object : fo) {
					writer = response.getWriter();
					flag++;
					int cid = Integer.parseInt(object.toString());
					deleteInfo= companyInfoService.deleteCompanySearchById(cid);
				   }
				if(flag==fo.size()){
						if(deleteInfo==1){
							writer.print("1");
							writer.flush();
						}
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					writer.close();
				}
		}
	}
	//查询长期招聘公司前台首页信息
	@RequestMapping(value="companyIndexWeb",produces="text/html;charset=UTF-8")
	public String companyIndexWeb1(Model model,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
			response.setContentType("text/plain; charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			String contextPath = request.getContextPath();
			String localPort = String.valueOf(request.getLocalPort());
			 //获取当前页数
            String pageNow=request.getParameter("pageNow");
            //获取总页数
            int totalCount=companyInfoService.findCompanyCount();
            PageUtil page=null;
            List<CompanyIndexInfo> list=new ArrayList<CompanyIndexInfo>();
            if (pageNow!=null) {
                page=new PageUtil(Integer.parseInt(pageNow), totalCount);
                list=companyInfoService.findCompanyPage(page.getStartPos(),page.getPageSize());
                for (CompanyIndexInfo cii : list) {
                	String cphoto = cii.getCphoto();
                	String concat = remoteAddr.concat(request.getContextPath());
                	String cphotoPath =concat.concat(cphoto);
                	cii.setCphoto(http.concat(cphotoPath));
                }
            }else {
                page=new PageUtil(1, totalCount);
                list=this.companyInfoService.findCompanyPage(page.getStartPos(),page.getPageSize());
                for (CompanyIndexInfo cii : list) {
                	String cphoto = cii.getCphoto();
                	String concat = remoteAddr.concat(request.getContextPath());
                	String cphotoPath =concat.concat(cphoto);
                	cii.setCphoto(http.concat(cphotoPath));
                }
            }
			model.addAttribute("companyIndex", list);
			model.addAttribute("page", page);
		    return "companyPre/LongTermIndex";
	}
	//查询长期招聘前台企业详细信息
	@RequestMapping(value="companyIndexDetail",produces="text/html;charset=UTF-8")
	public String companyIndexDetail(Model model,CompanyInfo ci,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
			response.setContentType("text/plain; charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			Integer cid = ci.getCid();
			CompanyInfo companyInfo=companyInfoService.findCompanyInfoByCid(cid);
			model.addAttribute("cidWeb", companyInfo);
			return "companyPre/LongTermIndex_detail";
	}
	
}
